import { UiButton } from "@/shared/Uikit";
import { useGetLessonById } from "@/entities/lesson/model/useGetLessonById";
import { useEffect, useState } from "react";
import { RawDraftContentState } from "draft-js";
import { useSaveLesson } from "../model/useSaveLesson";

type SaveLessonButtonProps = {
    lessonId: number,
    onSuccess?: () => void
    testIds: number[]
    content: RawDraftContentState
    onError?: (errorMessage: string) => void
}

export default function SaveLessonButton(props: SaveLessonButtonProps) {
    const saveLesson = useSaveLesson()
    useEffect(() => {
        if (saveLesson.errorMessage && props.onError) {
            props.onError(saveLesson.errorMessage)
        }
        if (saveLesson.isSuccess && props.onSuccess) {
            props.onSuccess()
        }

    }, [saveLesson.isSuccess, saveLesson.errorMessage])

    return (
        <>

            <UiButton variant='primary'
                onClick={() => {
                    saveLesson.handleSubmit({ lessonId: props.lessonId, testIds: props.testIds, content: props.content })
                }}
            >
                Сохранить урок
            </UiButton>
        </>
    )
}